package com.wjv.example.spbt.task.service;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpEntity;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpMethod;
import org.springframework.http.ResponseEntity;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;
import org.springframework.util.LinkedMultiValueMap;
import org.springframework.util.MultiValueMap;
import org.springframework.web.client.RestTemplate;

/**
 * <p>Title: MyTaskService</p>
 *
 * @author wjv
 * @date 2021/4/22 11:41
 * @description
 */
@Component
public class MyTaskService {
    private static Logger logger = LoggerFactory.getLogger(MyTaskService.class);

    @Autowired
    private RestTemplate restTemplate;

    // @Scheduled(cron = "12 0 0 * * ? ")
    public void work() {
        logger.debug("hello, world");
        doWork();
    }

    public void doWork() {
        String url = "http://sdk.gateway.yofijoy.com/mp/api/web/mp/mlbb/receiveGift";

        MultiValueMap<String, String> params = new LinkedMultiValueMap<>();
        params.add("openId", "ooqjDwqUiK68y63KOzgjMcu81tHk");
        params.add("goodsId", "1301");

        HttpEntity<MultiValueMap<String, String>> httpEntity = new HttpEntity<>(params, getHttpHeaders());

        ResponseEntity<String> res = restTemplate.exchange(url, HttpMethod.POST, httpEntity, String.class);

        logger.debug(res.getBody());
    }

    private HttpHeaders getHttpHeaders() {
        HttpHeaders headers = new HttpHeaders();
        headers.set("accept", "application/json, text/plain, */*");
        headers.set("accept-encoding", "gzip, deflate");
        headers.set("connection", "keep-alive");
        headers.set("cache-control", "no-cache");
        headers.set("content-type", "multipart/form-data; boundary=----WebKitFormBoundaryZhndwt4T6MRLhAw6");
        headers.set("host", "sdk.gateway.yofijoy.com");
        headers.set("Origin", "http://mp.pay.yofijoy.com");
        headers.set("Referer", "http://mp.pay.yofijoy.com/");
        headers.set("user-agent", "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/88.0.4324.182 Safari/537.36");
        return headers;
    }
}
